package logic

import (
	"context"
	"errors"
	"video-management-system/model"

	"video-management-system/userrpc/internal/svc"
	"video-management-system/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateVideoLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateVideoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateVideoLogic {
	return &UpdateVideoLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 修改视频状态
func (l *UpdateVideoLogic) UpdateVideo(in *userrpc.UpdateVideoRequest) (*userrpc.UpdateVideoResponse, error) {
	// todo: add your logic here and delete this line
	var findUser model.User

	if err := findUser.FindUserById(model.DB, in.Uid); err != nil {
		return nil, err
	}
	if findUser.ID == 0 {
		return nil, errors.New("用户不存在")
	}
	var findVideo model.VideoInfo

	if err := findVideo.FindVideoById(model.DB, in.Vid); err != nil {
		return nil, err
	}
	if findVideo.ID == 0 {
		return nil, errors.New("视频不存在")
	}
	findVideo.VideoStatus = in.VideoStatus

	if err := findVideo.UpdateVideo(model.DB, in.Vid); err != nil {
		return nil, errors.New("修改失败")
	}
	return &userrpc.UpdateVideoResponse{
		VideoId: int64(findVideo.ID),
	}, nil
}
